<template>
	<view class="pagecontent">
		<view class="row">标题：<input type="text" v-model="title" value="" /></view>
		<view class="row">内容：<textarea value="" v-model="content" placeholder="请输入内容" /></view>
		<view class="row">任务截止时间：
		  <picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
		      <view class="uni-input">选择时间{{date}}</view>
		  </picker>
		</view>
		<view class="row">执行人姓名：{{doman_input}}</view>
		<mix-tree :list="list" @treeItemClick="treeItemClick"></mix-tree>
		<button type="primary" @click="taskUpdate"v-if="updata">修改</button>
		<button type="warn" @click="taskRemove" v-if="updata">删除</button>
		<button type="primary" @click="tasksave" v-if="!updata">提交</button>
	</view>
</template>

<script>
	import mixTree from '@/components/mix-tree/mix-tree';
	export default {
		components: {
			mixTree
		},
		data() {
			return {
				list: [],
				
				title:'',
				content:'',
				doman:'',
				doman_input:'',
				date:'',
				
				id:'',
				updata:false
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad(option){
			this.getUnderUser();
			
			if(option.data){
				const item = JSON.parse(decodeURIComponent(option.data));
				this.id=item.id
				this.title=item.title
				this.content=item.content
				this.doman=item.doman
				this.doman_input=item.domanName
				this.date=item.overtime
				this.updata = true
			}
		},
		methods: {
			bindDateChange: function(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			
			//删除任务
			taskRemove(){
				
				var _this = this;
				uni.showModal({
					content:'是否要删除',
					success: (res) => {
						if(res.confirm){
							uni.request({
								url:_this.$API.taskRemove,
								method:"POST",
								data:{
									ids:_this.id //多个id用逗号隔开
								},
								header:_this.$API.get_head(),
								success: (res) => {
									uni.showToast({
										title:res.data.message,
										icon:'none'
									})
									if(res.data.success){
										uni.navigateBack({
											delta:1
										})
									}
								},
								fail: () => {
									uni.showToast({
										title:"加载失败",
										icon:'none'
									})
								}
							})
						}
					}
				})
			},
			//修改任务
			taskUpdate(){
				var _this = this;
				uni.request({
					url:this.$API.taskUpdate,
					method:"POST",
					data:{
						userid:uni.getStorageSync("userid"),// 用户id 
						'task.id':_this.id,
						'task.title':_this.title,
						'task.content':_this.content,
						'task.doman':_this.doman,
						'task.doman_input':_this.doman_input,
						'task.overtime':_this.date,
					},
					header:this.$API.get_head(),
					success: (res) => {
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
			},
			//发起任务
			tasksave(){
				if(!this.title||!this.content||(!this.doman&&this.doman!=0)||!this.doman_input||!this.date){
					uni.showToast({
						title:'请将表格填写完整',
						icon:'none'
					})
					return false;
				}
				var _this = this;
				uni.request({
					url:this.$API.tasksave,
					method:"POST",
					dataType:'json',
					data:{
						userid:uni.getStorageSync("userid"),// 用户id 
						'task.title':_this.title,
						'task.content':_this.content,
						'task.doman':_this.doman,
						'task.doman_input':_this.doman_input,
						'task.overtime':_this.date,
					},
					header:this.$API.get_head(),
					success: (res) => {
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
						if(res.data.success){
							_this.title='';
							_this.content='';
							_this.doman='';
							_this.doman_input='';
							_this.date='';
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
			},
			//获取4.执行人列表
			getUnderUser(){
				var _this = this;
				uni.request({
					url:this.$API.getUnderUser,
					method:"POST",
					data:{userid:uni.getStorageSync("userid")},
					header:this.$API.get_head(),
					success: (res) => {
						_this.list.push(res.data.data);
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
			},
			//点击最后一级时触发该事件
			treeItemClick(item) {
				let {
					id,
					name,
					parentId,
					type,
				} = item;
				// uni.showModal({
				// 	content: `点击了${parentId.length+1}级菜单, ${name}, id为${id}, 父id为${parentId.toString()}`
				// })
				var _this = this;
				if(item.type=='user'){
					uni.showModal({
						content: '是否选择该执行人',
						success:(res)=>{
							if(res.confirm){
								_this.doman = item.id
								_this.doman_input = item.name
							}
						},
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #FFFFFF;
	}
	.pagecontent{
		padding:20upx;
		.row{
			display: flex;
			padding:10upx 0;
			input{
				border: 1rpx solid #AAAAAA;
				border-radius:10upx;
			}
			textarea{
				border: 1rpx solid #AAAAAA;
				border-radius:10upx;
			}
			.uni-input{
				padding: 0;
			}
		}
		button{
			width:80%;
			margin:20upx auto 0;
		}
	}
	
</style>
